<template>
    <el-container class="home-container">
      <!-- 头部 -->
  <el-header>
    <div class="logo">
      <img src="../assets/heima.png" alt="">
      <span>电商后台管理系统</span>
    </div>
    <el-button type="info" plain @click="exit">退出</el-button>
  </el-header>
  <!-- 下方 -->
  <el-container >
    <!-- 左侧 -->
    <el-aside :width="width">
      <div class="openClose" @click="openClose">|||</div>
       <el-menu
      background-color="#313743"
      text-color="#fff"
      active-text-color="#409eff" :collapse-transition="false" :collapse="flage" :default-active="activePath" router unique-opened >
      <el-submenu :index="'/'+ item.path" v-for=" item in munList" :key="item.id" >
      <!--  router 是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转 -->
        <!-- 一级菜单 -->
        <template slot="title">
          <i :class="iconsObj[item.id]"></i>
          <span class="icof">{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
        <el-menu-item :index="'/'+ itemm.path" v-for=" itemm in item.children" :key="itemm.id" @click="choosePath('/'+ itemm.path)">
        <!-- <el-menu-item :index="'/'+ itemm.path+'/'+item.authName+'/'+itemm.authName" v-for=" itemm in item.children" :key="itemm.id" @click="choosePath('/'+ itemm.path)"> -->
        <!-- <el-menu-item :index="{ name:'user', props: {fname: item.authName, meName: itemm.authName}}" v-for=" itemm in item.children" :key="itemm.id" @click="choosePath('/'+ itemm.path)"> -->
          <template slot="title"  >
          <i class="el-icon-menu"></i>
          <span>{{itemm.authName}}</span>
        </template>
        </el-menu-item>
        </el-submenu>
    </el-menu>
    </el-aside>
    <el-main>
      <router-view/>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
export default {
  data () {
    return {
      munList: '',
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      flage: false, // 折叠标签
      width: '200px', // 左侧导航栏菜单
      activePath: '' // 当前选中的页签激活页签
    }
  },
  methods: {
    exit () {
      sessionStorage.clear()
      // sessionStorage.setItem('token', '')
      this.$router.push('/login')
    },
    async getMunlist () {
      const { data: res } = await this.$http.get('/menus')
      if (res.meta.status !== 200) return this.$message.error('获取列表失败')
      this.munList = res.data
    },
    openClose () {
      this.flage = !this.flage
      this.width = this.flage ? '64px' : '200px'
    },
    choosePath (path) { // 保存二级菜单的激活状态
      sessionStorage.setItem('path', path) // ssession保存
      this.activePath = path // 赋值给变量数据层和视图层绑定了，所以会变化
    }
  },
  created () {
    this.getMunlist()
    this.activePath = sessionStorage.getItem('path')
  },
  mounted () { // 页面一渲染 查看session中是否有保存的path
    if (!this.activePath) return this.$router.push('/welcome') // 判断计入后是否有path的session，没有的话代表第一次进来或者是刚点击了首页按钮
    if (location.hash.slice(1) === this.activePath) return
    this.$router.push(this.activePath)
  }
}
</script>

<style lang="less" scoped>
.el-header {
  display: flex;
  justify-content: space-between;
  background-color: #363c42;
  color: #fff;
  font-size: 20px;
  >div {
    display: flex;
    align-items: center;
    >span {
      padding-left: 10px;
    }
  }
}
.el-aside {
  background-color: #313743;
}
.el-main {
  background-color: #e9edf1;
}
.home-container {
  height: 100%;
}
.icof {
  margin-left:10px ;
}
.openClose {
    background-color: #4a5064;
    color: #fff;
    font-size: 12px;
    text-align: center;
    line-height: 22px;
    height: 22px;
    cursor: pointer;
    letter-spacing: .12em;

}
.el-menu {
  border-right: none;
}
</style>
